import Cookies from "js-cookie";
import { clearLoginInfo } from '@/utils';

/**
 * 全站路由配置
 */
import { createRouter, createWebHistory } from 'vue-router'

// 全局路由
const globalRoutes = [
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/common/login.vue'),
        meta: {
            title: '登录'
        }
    },
    {
        path: '/404',
        name: '404',
        component: () => import('@/views/common/404.vue'),
        meta: {
            title: '404-页面不存在'
        }
    },
]

// 主入口路由
const mainRoutes = {
    path: '/',
    component: () => import('../views/main.vue'),
    name: 'main',
    meta: {
        title: '主入口布局'
    },
    children: [
        {
            path: '/home',
            name: 'home',
            component: () => import('../views/common/home.vue'),
            meta: {
                title: '系统首页'
            }
        }
    ],
    beforeEnter: (to: any, from:  any, next: any) => {
        let token = Cookies.get('token');
        console.log('token: ' + token);
        if (!token) {
        // if (!token || !/\s/.test(token)) {
            clearLoginInfo();
            next({
                name: 'login'
            });
        } else {
            next();
        }
    }
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: globalRoutes.concat(mainRoutes),
})

export default router
